<?php

namespace app\modules\cruise\models;

use Yii;

/**
 * This is the model class for table "room_use".
 *
 * @property int $id
 * @property string $room_id 舱房编号
 * @property string $num 舱房入住人数
 * @property string $code 房间号
 * @property int $download 是否下载 0 修改  1 已下载
 * @property int $status 1正常  2禁用
 * @property int $user_id 管理员编号
 * @property string $created_at
 * @property string $updated_at
 */
class RoomUse extends \yii\db\ActiveRecord
{
    public  $num_s;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'room_use';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['room_id'], 'required'],
            [['download', 'status', 'user_id','room_id','num','ord'], 'integer'],
            [['created_at', 'updated_at','remark'], 'safe'],
            [['code'], 'string', 'max' => 16],
        ];
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }

        return parent::beforeSave($insert);
    }//}}}

     // 保存后执行
    public function afterSave($insert, $changedAttributes){//{{{

        parent::afterSave($insert, $changedAttributes);

        if($insert) {

        }else{
            if(isset($changedAttributes['code'])){
                if($this->download == 2){
                    $this->download = 1;
                    $this->save();
                }
            }
        }

    }//}}}

    //  获取对应房间
    public function getRoom(){
        return $this->hasOne(Room::className(), ['id' => 'room_id']);
    }

    // 获取房间入住人
    public function getCustomer(){
        return $this->hasMany(Customer::className(), ['room_use_id' => 'id'])->onCondition(['status'=>1]);
    }

    // 获取房间入住人
    public function getAllCustomer(){
        return $this->hasMany(Customer::className(), ['room_use_id' => 'id'])->onCondition(['status'=>1,'cancel'=>1]);
    }

    //获取房间入住人
    public function getSearchCustomer(){
        return $this->hasMany(Customer::className(), ['room_use_id' => 'id'])->onCondition(['customer.status'=>1]);
    }

    //获取房间入住人数量
    public function getCustomerNum(){
        return $this->hasMany(Customer::className(), ['room_use_id' => 'id'])->onCondition(['status'=>1])->count();
    }

    public function getCustomerNums(){
        return $this->hasMany(Customer::className(), ['room_use_id' => 'id'])->onCondition(['status'=>1,'cancel'=>1])->count();
    }

    //获取 同房间类型 和 入住人员 的总人数
    public function getRoomNumCustomerNum(){
        $room_use = RoomUse::find()->where(['room_id'=>$this->room_id,'num'=>$this->num,'status'=>1])->all();
        $all_num = 0;
        if($room_use){
            foreach($room_use as $ru){
                $all_num += $ru->customerNum;
            }
        }
        return $all_num;
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'room_id' => '房间类型',
            'num' => '入住人数',
            'code' => '房间号',
            'download' => '下载状态',
            'status' => '状态',
            'remark' => '备注',
            'ord' => '代码序号',
            'user_id' => 'User ID',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
}
